<?php

namespace app\admin\controller;

use app\common\controller\Backend;
use app\admin\model\PriceList as PriceListModel;

/**
 * 物料申购单
 *
 * @icon fa fa-circle-o
 */
class MaterialPurchaseRequest extends Backend
{

    /**
     * MaterialPurchaseRequest模型对象
     * @var \app\admin\model\MaterialPurchaseRequest
     */
    protected $model = null;

    public function _initialize()
    {
        parent::_initialize();
        $this->model = new \app\admin\model\MaterialPurchaseRequest;
        $this->view->assign("materialTypeList", $this->model->getMaterialTypeList());
    }


    // public function add(){
    //     $params = $this->request->param();
    //     return $params;
    // }
    // 返回申购单 信息
    public function getMaterialPurchaseRequestInfo()
    {
        $params = $this->request->post();
        $response = ['total' => 0, 'rows' => []];
        if (isset($params['searchValue'])) {
            $data = $this->model->where('id', $params['searchValue'])->select();
            $response['total'] = 1;
            $response['rows'] = $data;
            return json($response);
        }
        $data = $this->model;
        if (!empty($params['searchKey'])) {
            $data = $data->where('name', 'like', '%' . $params['searchKey'] . '%'); // 假设根据name字段搜索
        }
        $data = $data->select();
        $response['total'] = count($data);
        $response['rows'] = $data;
        return json($response);
    }

    
    // 返回申购单 物料价格
    public function getMaterialPrice()
    {
        $priceListModel = new PriceListModel();
        $params = $this->request->post();
        $supplier_id = $params['supplier_id'];
        $material_id = $params['material_id'];
        $result = ['total'=> 0,'rows'=> []];
        $rows = $priceListModel->where(["supplier_id"=>$supplier_id,"material_id"=>$material_id])->select();
        $result["total"] = count($rows);
        $result["rows"] = $rows;
        return json($result);
    }

    /**
     * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个基础方法、destroy/restore/recyclebin三个回收站方法
     * 因此在当前控制器中可不用编写增删改查的代码,除非需要自己控制这部分逻辑
     * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
     */


}
